package fr.uniscol.editors;

import java.beans.PropertyEditorSupport;

import fr.uniscol.model.Departement;
import fr.uniscol.service.IGeographieService;

public class DepartementPropertyEditor extends PropertyEditorSupport {

	private IGeographieService geoService;
	
	public DepartementPropertyEditor(IGeographieService geoService){
		this.geoService = geoService;
	}
	
	@Override
	public String getAsText() {
		
		Departement d = (Departement) getValue();
		
		String text = d.getId() + "";
		
		return text;
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		
		int departementId = Integer.parseInt(text);
		
		setValue(geoService.getDepartementById(departementId));
	}
}
